import  React,{ Component,PropTypes } from 'react';
import {
    StyleSheet,
    View,
    Image,
    Text,
    TextInput,
    TouchableOpacity,
    TouchableNativeFeedback,
    Platform
} from 'react-native';

export default class Touch extends Component {
    // 构造
    constructor(props) {
        super(props);
        // 初始状态
        this.state = {
            isDisable:false,//是否被禁用
        };
    }
    componentWillMount() {

    }

    componentDidMount(){
        this._mounted = true;
    }

    componentWillUnmount() {
        this._mounted = false;
        this.timer && clearTimeout(this.timer);
    }

    async ToPress(){
        const {onPress} = this.props;
        // this.timer && clearTimeout(this.timer);

        if(this._mounted){
            await this.setState({isDisable:true});//防重复点击
            onPress && onPress();

            this.timer = setTimeout(async()=>{
                await this.setState({isDisable:false});//秒后可点击
            },900);
        }

    }

    render(){
        const {style, activeOpacity, children, accessible} = this.props;

        return(
            <TouchableOpacity
                disabled={this.state.isDisable}
                activeOpacity={activeOpacity?activeOpacity:0.7}
                style={style?style:{}}
                accessible={accessible?true:false}
                onPress={this.ToPress.bind(this)}>
                {children}
            </TouchableOpacity>
        );
    }
}
